package com.pro01.backend.controller;

import com.pro01.backend.pojo.Result;
import com.pro01.backend.service.UserInviteService;
import com.pro01.backend.utils.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/invites")
public class AdminInviteController {

    @Autowired
    private UserInviteService userInviteService;

    // ✅ 管理员才能生成邀请码
    @PostMapping("/generate")
    public Result<String> generateInviteByAdmin(HttpServletRequest request) {
        Long userId = TokenUtil.getUserId(request);
        String role = TokenUtil.getRole(request);
        if (!"ADMIN".equals(role)) {
            return Result.error("权限不足，只有管理员可以生成邀请码");
        }

        String inviteCode = userInviteService.generateInvite(userId);
        return Result.success(inviteCode);
    }

}

